package com.web.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Pile;
import com.common.entity.Site;
import com.common.entity.Supplier;
import com.web.main.entity.form.PileForm;
import com.web.main.entity.param.PileQuery;
import com.web.main.entity.vo.PileVo;
import com.web.main.entity.vo.SupplierVo;
import com.web.main.exception.PileException;
import com.web.main.exception.SiteException;
import com.web.main.mapper.PileMapper;
import com.web.main.mapper.SiteMapper;
import com.web.main.mapper.SupplierMapper;
import com.web.main.service.PileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
@Service
public class PileServiceImpl extends ServiceImpl<PileMapper, Pile> implements PileService {
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    SiteMapper siteMapper;
    /**
     * 添加电桩
     * @param pileForm
     */
    @Override
    public void addPile(PileForm pileForm) {
        if(ObjectUtils.isEmpty(pileForm)) {
            throw new PileException("pile is null");
        }
        //先查数据库，供应商是否存在
        Supplier supplier = supplierMapper.selectById(pileForm.getSupplierId());
        if(ObjectUtils.isEmpty(supplier)) {
            throw new PileException("supplier does not exist");
        }
        //电站是否存在
        Site site = siteMapper.selectById(pileForm.getSiteId());
        if(ObjectUtils.isEmpty(site)) {
            throw new PileException("site does not exist");
        }

        //拷贝前端对象属性值
        Pile pile = new Pile();
        BeanUtils.copyProperties(pileForm, pile);

        pile.setPileId(null);  //将电桩主键置为null
        pile.setPileState(null);  //将电桩状态置为null

        int insert = baseMapper.insert(pile);

        if(insert == 0) {
            throw new PileException("add pile fail");
        }
    }

    /**
     * 删除电桩
     * @param pileId
     */
    @Override
    public void removePileById(String pileId) {
        if(StringUtils.isBlank(pileId)) {
            throw new PileException("remove fail with pileId is null");
        }
        //先查询，在修改
        Pile pile = selectEqIdNeState(pileId);
        //将状态修改为-1
        pile.setPileState(-1);
        int i = baseMapper.updateById(pile);
        if(i == 0) {
            throw new PileException("remove pile fail with id：" + pileId);
        }
    }

    /**
     * 修改电桩
     * @param pileForm
     */
    @Override
    public void modifyPile(PileForm pileForm) {
        if(ObjectUtils.isEmpty(pileForm)) {
            throw new PileException("modify Pile fail with param is null");
        }

        //先查询。已经非空判断
        Pile pile = selectEqIdNeState(pileForm.getPileId());

        String supplierId = pileForm.getSupplierId();
        String siteId = pileForm.getSiteId();
        Integer pileType = pileForm.getPileType();
        Integer pileModel = pileForm.getPileModel();
        Integer pileState = pileForm.getPileState();

        if(!ObjectUtils.isEmpty(supplierId)) {
            pile.setSupplierId(supplierId);
        }
        if(!ObjectUtils.isEmpty(siteId)) {
            pile.setSiteId(siteId);
        }
        if(!ObjectUtils.isEmpty(pileType)) {
            pile.setPileType(pileType);
        }
        if(!ObjectUtils.isEmpty(pileModel)) {
            pile.setPileModel(pileModel);
        }
        if(!ObjectUtils.isEmpty(pileState)) {
            pile.setPileState(pileState);
        }
        //再修改
        int i = baseMapper.updateById(pile);

        if(i == 0) {
            throw new PileException("modify fail with pile" + pileForm);
        }
    }

    /**
     * 根据id查电桩
     * @param pileId
     */
    @Override
    public PileVo getPileById(String pileId) {
        if(StringUtils.isBlank(pileId)) {
            throw new PileException("select pile fail with pileId is null");
        }

        //查询。条件，id相同，状态不为-1.
        Pile pile = selectEqIdNeState(pileId);

        PileVo pileVo = new PileVo();
        BeanUtils.copyProperties(pile, pileVo);   //pileVo属性赋值

        //根据【供应商id】查供应商（供应商已经做了软删除，所以不需要使用条件查询）
        Supplier supplier = supplierMapper.selectById(pileVo.getSupplierId());
        if(ObjectUtils.isEmpty(pile)) {
            throw new PileException("select fail with supplier is null");
        }

        SupplierVo supplierVo = new SupplierVo();
        BeanUtils.copyProperties(supplier, supplierVo);  //supplierVoForm属性赋值

        //将供应商信息赋值给电桩
        pileVo.setSupplierVo(supplierVo);
        return pileVo;
    }

    /**
     * 分页查电桩
     * @param pileQuery  条件对象
     */
    @Override
    public Page<PileVo> pageQuery(PileQuery pileQuery) {
        Integer page = pileQuery.getPage();
        Integer limit = pileQuery.getLimit();
        //分页查，查电桩状态：不是审批和删除的
        Page<Pile> pilePage = new Page<>(page, limit);
        QueryWrapper<Pile> queryWrapper = new QueryWrapper<>();

        //要排除状态为-1的
        queryWrapper.ne("pile_state", -1);

        if(!ObjectUtils.isEmpty(pileQuery)) {  //条件对象不为空
            Integer pileType = pileQuery.getPileType();
            Integer pileModel = pileQuery.getPileModel();

            if(!ObjectUtils.isEmpty(pileType)) {
                queryWrapper.eq("pile_type", pileType);
            }
            if(!ObjectUtils.isEmpty(pileModel)) {
                queryWrapper.eq("pile_model", pileModel);
            }
        }
        //分页查
        baseMapper.selectPage(pilePage, queryWrapper);
        //属性赋值
        Page<PileVo> pileVoPage = new Page<>();
        BeanUtils.copyProperties(pilePage, pileVoPage);
        //需要重新当前页信息
        List<PileVo> pileVos = new ArrayList<>();
        for (int i = 0; i < pilePage.getRecords().size(); i++) {
            Pile pile = pilePage.getRecords().get(i);
            PileVo pileVo = new PileVo();
            BeanUtils.copyProperties(pile, pileVo);
            pileVos.add(pileVo);
        }
        pileVoPage.setRecords(pileVos);
        //填充供应商
        List<PileVo> records = pileVoPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            PileVo pileVo = records.get(i);

            //查供应商表。使用条件查。
            QueryWrapper<Supplier> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("supplier_id", pileVo.getSupplierId());
            //不能直接将条件添加到连表查询中，这样只会将另外的表查不出来。
//            if(!ObjectUtils.isEmpty(pileQuery)) {  //条件对象不为空
//                String supplierName = pileQuery.getSupplierName();
//                if(!StringUtils.isBlank(supplierName)) {
//                    queryWrapper1.eq("supplier_name", supplierName);  //供应商名字。条件查
//                }
//            }
            Supplier supplier = supplierMapper.selectOne(queryWrapper1);
            //属性值拷贝
            SupplierVo supplierVo = new SupplierVo();
            BeanUtils.copyProperties(supplier, supplierVo);
            //装配
            pileVo.setSupplierVo(supplierVo);
        }
        return pileVoPage;
    }

    /**
     * 抽取方法，根据主键查询状态不为-1的。
     * @param pileId
     * @return
     */
    private Pile selectEqIdNeState(String pileId) {
        QueryWrapper<Pile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pile_id", pileId);
        queryWrapper.ne("pile_state", -1);
        Pile pile = baseMapper.selectOne(queryWrapper);

        if(ObjectUtils.isEmpty(pile)) {
            throw new SiteException("pile does not exits");
        }
        return pile;
    }


}


